package cn.lll.vuehouse.controller;

import cn.lll.vuehouse.DTO.SaleDTO;
import cn.lll.vuehouse.FORM.SaleForm;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.entity.Goods;
import cn.lll.vuehouse.entity.Sales;
import cn.lll.vuehouse.entity.User;
import cn.lll.vuehouse.mapper.GoodsMapper;
import cn.lll.vuehouse.mapper.NoticeMapper;
import cn.lll.vuehouse.service.CustomerService;
import cn.lll.vuehouse.service.GoodsService;
import cn.lll.vuehouse.service.SalesService;
import cn.lll.vuehouse.service.UserService;
import cn.lll.vuehouse.utils.JWTUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@RestController
@RequestMapping("sale")
public class SaleController {

    @Autowired
    private SalesService salesService;

    @Autowired
    private GoodsService GoodsService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private UserService userService;

    @GetMapping("list")
    public ResponseResult getList(SaleDTO saleDTO){
        return salesService.getList(saleDTO);
    }

    // 回显编辑弹窗
    @GetMapping("getSaleById")
    public ResponseResult getSaleById(Integer id){
        return salesService.getSaleById(id);
    }

    // 删除
    @DeleteMapping("deleteSaleById")
    public ResponseResult deleteSaleById(Integer id){
        ResponseResult responseResult = new ResponseResult();
        salesService.removeById(id);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

    // 退货
    @GetMapping("back")
    public ResponseResult getBack(Integer id){
        return salesService.getBack(id);
    }

    // 添加修改
    @PostMapping("addOrUpdateSale")
    public ResponseResult addOrUpdateSale(@RequestBody Sales sales, HttpServletRequest request){
        ResponseResult responseResult = new ResponseResult();
        Integer number = sales.getNumber();
        Integer goodsid = sales.getGoodsid();
        Goods goods = GoodsService.getById(goodsid);
        Integer number1 = goods.getNumber();

        // 判断是修改还是添加
        if (ObjectUtils.isNotEmpty(sales.getId())){
            Sales byId = salesService.getById(sales.getId());
            Integer number2 = byId.getNumber();
            // 删除goods表中商品数量,先加改之前在减改之后
            goods.setNumber(number1+number2-number);
            GoodsService.updateById(goods);
            salesService.saveOrUpdate(sales);
            responseResult.success(ResponseStatus.SUCCESS);
            return responseResult;
        }

        // 删除goods表中商品数量
        goods.setNumber(number1-number);
        GoodsService.updateById(goods);

        // 获取当前用户信息
        String token = request.getHeader("token");
        Integer userId = (Integer) JWTUtil.getClaim(token).get("userId");
        User byId = userService.getById(userId);
        String name = byId.getName();
        sales.setOperateperson(name);
        // 获取销售额
        Double price = goods.getPrice();
        BigDecimal saleprice = new BigDecimal(Double.toString(number*price));
        sales.setSaleprice(saleprice);
        salesService.saveOrUpdate(sales);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }
}
